import util from 'util.js';

var isRelease = 'prd'; // prd:正式服； test:测试服;  dev:开发服;

var gatewayUrl;
if (isRelease == 'prd') {
  gatewayUrl = 'https://api.xtqtech.com'
} else {
  gatewayUrl = 'https://api-' + isRelease + '.xtqtech.com'
}

var wsIsConn = false

var second = 50
var interval

/**
 * 请求api接口
 * url     地址
 * success 成功的回调
 * fail    失败的回调
 */
function _postCoreApi(url, data, success, fail) {
  var ur = gatewayUrl + '/coreApi/apiBean/' + url;
  _basePost(ur, data, success, fail);
}

/**
 * 请求api接口
 * url     地址
 * success 成功的回调
 * fail    失败的回调
 */
function _post(url, data, success, fail) {
  var ur = gatewayUrl + '/consumerApi/' + url;
  _basePost(ur, data, success, fail);
}

function _get(url, data, success, fail) {
  var ur = gatewayUrl + '/consumerApi/' + url;
  _baseGet(ur, data, success, fail);
}

/**
 * 支付
 */
function _postPay(url, data, success, fail) {
  var ur = gatewayUrl + '/coreApi/pay/' + url;
  _basePost(ur, data, success, fail);
}

/**
 * deviceApi post
 */
function _postDeviceApi(url, data, success, fail) {
  var ur = gatewayUrl + '/deviceApi' + url;
  _basePost(ur, data, success, fail);
}

/**
 * gateway get
 */
function _getGatewayApi(url, data, success, fail) {
  var ur = gatewayUrl + url;
  _baseGet(ur, data, success, fail);
}

/**
 * gateway post
 */
function _postGatewayApi(url, data, success, fail) {
  var ur = gatewayUrl + url;
  _basePost(ur, data, success, fail);
}

/**
 * merchant post
 */
function _postMerchantApi(url, data, success, fail) {
  var ur = gatewayUrl + '/merchantApi' + url;
  _basePost(ur, data, success, fail);
}

/**
 * merchant get
 */
function _getMerchantApi(url, data, success, fail) {
  var ur = gatewayUrl + '/merchantApi' + url;
  _baseGet(ur, data, success, fail);
}

/**
 * 基础的请求接口 post
 * url     地址
 * success 成功的回调
 * fail    失败的回调
 */
function _basePost(url, data, success, fail) {
  wx.request({
    url: url,
    header: {
      'content-type': 'application/json',
      'X-Platform-With': '3'
    },
    method: 'POST',
    data: data,
    success: function (res) {
      success(res);
    },
    fail: function (res) {
      fail(res);
    }
  });
}

/**
 * 基础的请求接口 get
 * url     地址
 * success 成功的回调
 * fail    失败的回调
 */
function _baseGet(url, data, success, fail) {
  wx.request({
    url: url,
    header: {
      'content-type': 'application/json',
      'X-Platform-With': '3'
    },
    method: 'GET',
    data: data,
    success: function (res) {
      success(res);
    },
    fail: function (res) {
      fail(res);
    }
  });
}

/**
 * 推送长连接
 */
function linkWebSocket(success) {
  if (wsIsConn) {
    wx.closeSocket()
  }
  wx.getStorage({
    key: 'loginInfo',
    success: function (res) {
      var union_id = res.data.userInfo.unionId
      wx.onSocketOpen(function (res) {
        console.log('WebSocket 已打开')
        wsIsConn = true
        wx.sendSocketMessage({
          data: union_id
        })
        interval = setInterval(function () {
          second--
          if (second <= 0) {
            second = 50
            wx.sendSocketMessage({
              data: 'ping'
            })
          }
        }, 1000)
      })
      wx.onSocketMessage(function (res) {
        success(res)
      })
      wx.onSocketClose(function (res) {
        console.log('WebSocket 已关闭')
        wsIsConn = false
        clearInterval(interval)
      })
      wx.onSocketError(function (res) {
        console.log('WebSocket连接打开失败，请检查！')
      })
    }
  })
}

module.exports = {
  isRelease: isRelease,
  _post: _post,
  _get: _get,
  _postCoreApi: _postCoreApi,
  linkWebSocket: linkWebSocket,
  _postPay: _postPay,
  _postDeviceApi: _postDeviceApi,
  _postGatewayApi: _postGatewayApi,
  _getMerchantApi: _getMerchantApi,
  _postMerchantApi: _postMerchantApi
}